package com.cmys.money.cloudshop.service.product;

import imageUtil.Image;
import imageUtil.ImageLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cmys.money.cloudshop.dao.product.BrandDao;
import com.cmys.money.cloudshop.dao.product.ProductInfoDao;
import com.cmys.money.cloudshop.dao.product.ProductTypeDao;
import com.cmys.money.cloudshop.model.product.Brand;
import com.cmys.money.cloudshop.model.product.ProductInfo;
import com.cmys.money.cloudshop.model.product.ProductType;
import com.cmys.money.cloudshop.model.system.File;
import com.cmys.money.cloudshop.model.system.ImageFile;
import com.cmys.money.cloudshop.util.file.FileBean;
import com.cmys.money.cloudshop.util.file.FileUtils;

@Service("productService")
public class ProductServiceImpl implements ProductService {

	protected final Log logger = LogFactory.getLog(getClass());
	
	// 性能会更好
	private final BrandDao brandDao;
	private final ProductTypeDao productTypeDao;
	private final ProductInfoDao productInfoDao;
	
	@Autowired
	public ProductServiceImpl(BrandDao brandDao, ProductTypeDao productTypeDao,
			ProductInfoDao productInfoDao) {
		super();
		this.brandDao = brandDao;
		this.productTypeDao = productTypeDao;
		this.productInfoDao = productInfoDao;
	}
	
	@Override
	@Transactional
	public Brand addBrand(Brand brand){
		return brandDao.save(brand);
	}

	@Override
	public ProductType findProductTypeTree(Long id) {
		return productTypeDao.findById(id);
	}

	@Override
	@Transactional
	public ProductInfo addProductInfo(ProductInfo productInfo) {
		return productInfoDao.save(productInfo);
	}

	@Override
	public ProductType findProductTypeNavigation(Long id) {
		return productTypeDao.findById(id);
	}
	
	@Override
	@Transactional
	public ProductInfo updateProductInfo(ProductInfo productInfo, FileBean fileBean, String path) throws Exception{
		productInfo = productInfoDao.findOne(productInfo.getId());
		// 原始图片
		File file = new File();
		// 图片名称d
		String oname = fileBean.getName();
		file.setFilename(oname);
		// 原始图片路径
		String opath = fileBean.getPath();
		// 原始图片的格式
		String oformat = fileBean.getExt();
		file.setFilepath(opath);
		Image image = ImageLoader.fromFile(path + java.io.File.separator + opath);
		ImageFile imageFile = new ImageFile(image.getWidth(), image.getHeight(), oformat, file);
		
		// 商品前台图片 160
		String rfolder1 = FileUtils.getRondomFolderPath();
		String dpath1 = rfolder1 + java.io.File.separator + FileUtils.getRondomFileName(oname);
		Image image1 = image.getResizedToWidth(160);
		FileUtils.createFolder(new java.io.File(path + rfolder1)); // 先创建路径
		image1.soften(0.08f).writeToJPG(new java.io.File(path + dpath1), 0.95f);
		File file1 = new File();
		file1.setFilename(oname);
		file1.setFilepath(dpath1);
		ImageFile imageFile1 = new ImageFile(image1.getWidth(), image1.getHeight(), "jpg", file1);

		productInfo.addImageFile(imageFile);
		productInfo.addImageFile(imageFile1);
		
		return productInfoDao.save(productInfo);
	}
	
//	// 商品浏览大图片
//			String rfolder2 = FileUtils.getRondomFolderPath();
//			String dpath2 = rfolder2 + java.io.File.separator + FileUtils.getRondomFileName(oname);
//			Image image2 = image.getResizedToWidth(600);
//			FileUtils.createFolder(new java.io.File(path + rfolder2)); // 先创建路径
//			image2.soften(0.08f).writeToJPG(new java.io.File(path + dpath2), 0.95f);
//			File file2 = new File();
//			file2.setFilename(oname);
//			file2.setFilepath(dpath2);
//			ImageFile imageFile2 = new ImageFile(image2.getWidth(), image2.getHeight(), "jpg", file2);
}
